package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import java.util.Vector;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Display an account list for user.
 */
public class CusConfirmServlet_v1r00 extends EbppxTransactionServlet {

    /**
     * Load vector of accounts into request.
     */
    public void doTransaction(
    HttpServletRequest req,
    HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);

            if (null == session)
                throw new IllegalStateException("no session established");

            final EbppxUrlManager urlManager = getUrlManager(req);

            // get accounts from session and filter out nulls
            final Vector vecAccounts = new Vector();
            final Vector vecBillSubscriptions = new Vector();
            final Vector vecAccountDetails = new Vector();
            String strPhysicalBillSubscription = "";

            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
            final Vector cfiAccounts = ecsUser.getAccounts();
            final Vector vecAccCompares = new Vector();

            final int size = cfiAccounts.size();

            String strEnableButton = (String)session.getAttribute(EbppxConstants.BUTTON_ENABLED);

            if(strEnableButton.equals("1")){// 1 for normal customer

	            String[] arrTxtNotifyViaSMS = req.getParameterValues("txtNotifyViaSMS");
	            String[] arrTxtBillEMail = req.getParameterValues("txtBillEMail");
	            String[] arrSelPrefix = req.getParameterValues("selPreFix");
	            String strCategoryDatabase = EbppxCusDBUtil.getCategoryDatabase();
	            String cusDatabase = "";


	            for(int i=0;i<size;i++){
	                final Account cfiAccount = (Account) cfiAccounts.elementAt(i);
	                final String strAcctExtKey = cfiAccount.getExternalKey();
	                final AcctDetails acctDetails = new AcctDetails();

	                EbppxAcctDetailsDataBean acctDataBean = new EbppxAcctDetailsDataBean();
	                EbppxAcctDetailsDataBean acctCompareDataBean = new EbppxAcctDetailsDataBean();

		            cusDatabase = EbppxCusDBUtil.getCusDatabase(strCategoryDatabase,strAcctExtKey);
					strPhysicalBillSubscription = EbppxCusDBUtil.processCUSPhysicalBillSubscription(strAcctExtKey,cusDatabase,strCategoryDatabase);

	                acctDataBean = acctDetails.getAcctDetails(strAcctExtKey);
					acctDataBean.setXPhysicalBill(strPhysicalBillSubscription);

					acctCompareDataBean.setAcctId(strAcctExtKey);
					acctCompareDataBean.setSMSSubscription(urlManager.getSecureParameter("cboSMSNotify"+i));
					acctCompareDataBean.setEmailSubscription(urlManager.getSecureParameter("cboBillEMail"+i));
					System.out.println("\nNew SMS No in servelt is "+arrTxtNotifyViaSMS[i]);

					acctCompareDataBean.setSMSNo(arrTxtNotifyViaSMS[i]);
					acctCompareDataBean.setEmailAdd(arrTxtBillEMail[i]);
					acctCompareDataBean.setAcctSubscription(urlManager.getSecureParameter("cboAccount"+i));
					acctCompareDataBean.setXPhysicalBill(urlManager.getSecureParameter("cboXPhysicalBill"+i));
					acctCompareDataBean.setPrefix(arrSelPrefix[i]);

					System.out.println("SMS subscription is..."+acctCompareDataBean.getEmailSubscription());


	                if (cfiAccount!=null){
	                    vecAccounts.addElement(cfiAccount);
		                vecAccCompares.addElement(acctCompareDataBean);
						vecAccountDetails.addElement(acctDataBean);
	                }
	            }
			}else{// 0 for corporate customer
	            String strCategoryDatabase = EbppxCusDBUtil.getCategoryDatabase();
	            String cusDatabase = "";


	            for(int i=0;i<size;i++){
	                final Account cfiAccount = (Account) cfiAccounts.elementAt(i);
	                final String strAcctExtKey = cfiAccount.getExternalKey();
	                final AcctDetails acctDetails = new AcctDetails();

	                EbppxAcctDetailsDataBean acctDataBean = new EbppxAcctDetailsDataBean();
	                EbppxAcctDetailsDataBean acctCompareDataBean = new EbppxAcctDetailsDataBean();

		            cusDatabase = EbppxCusDBUtil.getCusDatabase(strCategoryDatabase,strAcctExtKey);
					strPhysicalBillSubscription = EbppxCusDBUtil.processCUSPhysicalBillSubscription(strAcctExtKey,cusDatabase,strCategoryDatabase);

	                acctDataBean = acctDetails.getAcctDetails(strAcctExtKey);
					acctDataBean.setXPhysicalBill(strPhysicalBillSubscription);

					acctCompareDataBean.setAcctId(strAcctExtKey);
					acctCompareDataBean.setAcctSubscription(urlManager.getSecureParameter("cboAccount"+i));
					acctCompareDataBean.setXPhysicalBill(urlManager.getSecureParameter("cboXPhysicalBill"+i));

					acctCompareDataBean.setSMSSubscription("");
					acctCompareDataBean.setEmailSubscription("");

					acctCompareDataBean.setSMSNo("");
					acctCompareDataBean.setEmailAdd("");
					acctCompareDataBean.setPrefix("");

					System.out.println("SMS subscription is..."+acctCompareDataBean.getEmailSubscription());

	                if (cfiAccount!=null){
	                    vecAccounts.addElement(cfiAccount);
		                vecAccCompares.addElement(acctCompareDataBean);
						vecAccountDetails.addElement(acctDataBean);
	                }
	            }
			}

            req.setAttribute(EbppxConstants.CFI_ACCOUNTS, vecAccounts);
            req.setAttribute(EbppxConstants.NEW_ACCOUNTS, vecAccCompares);
            req.setAttribute(EbppxConstants.ACCT_DETAILS, vecAccountDetails);
            forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);

        }
        catch (Throwable t) {
            throw new ServletException(t);
        }
    }
}
